package cn.aaa.openFeign.impl;

import cn.aaa.domain.RuleVo;
import cn.aaa.exception.GlobalException;
import cn.aaa.mapper.ChargeRuleReturnMapper;
import cn.aaa.mapper.ChargeRuleStartMapper;
import cn.aaa.openFeign.RuleOpenFeign;
import cn.aaa.pojo.domain.ChargeRuleReturn;
import cn.aaa.pojo.domain.ChargeRuleStart;
import cn.aaa.result.JSONResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@RestController
public class RuleOpenfeignImpl implements RuleOpenFeign {
    @Autowired
    private ChargeRuleReturnMapper chargeRuleReturnMapper;//返程的配置

    @Autowired
    private ChargeRuleStartMapper chargeRuleStartMapper;
    @Override
    public JSONResult pricing(RuleVo ruleVo) {
        BigDecimal totalFee = new BigDecimal(0.0);
        //返程费用的计算
        ChargeRuleReturn chargeRuleReturn = chargeRuleReturnMapper.selectById(1L);//返程费用计价规则
        BigDecimal bigDecimal = new BigDecimal(ruleVo.getMileage());
        BigDecimal subtract = bigDecimal.subtract(chargeRuleReturn.getFreeBaseReturnMileage());
        if(subtract.intValue()>0){
            BigDecimal multiply = subtract.multiply(chargeRuleReturn.getExceedEveryKmAmount());
            totalFee = totalFee.add(multiply);
        }
        //代驾去终点的费用
        int hours = new Date().getHours();
        ChargeRuleStart chargeRuleStart = chargeRuleStartMapper.selectOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .le(ChargeRuleStart::getHourStart, hours)
                .ge(ChargeRuleStart::getHourEnd, hours));
        if(chargeRuleStart == null){
            throw new GlobalException("服务器异常");
        }
        totalFee = totalFee.add(chargeRuleStart.getAmount());//基础费用
        BigDecimal subtract1 = bigDecimal.subtract(chargeRuleStart.getBaseMileage());
        if(subtract1.intValue()>0){
            //超出的费用
            BigDecimal multiply = subtract1.multiply(chargeRuleStart.getExceedEveryKmAmount());
            totalFee = totalFee.add(multiply);
        }
        return JSONResult.success(totalFee.toString());
    }
}